<?php

/**
 * slot actions.
 *
 * @package    emc
 * @subpackage slot
 * @author     Your display_name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class slotActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  public function executeList(sfWebRequest $request)
  {
    $this->notify = $request->getParameter('notify');
    
    $con = Doctrine_Manager::getInstance()->connection();
    $parent_id = $request->getParameter('parent_id', null);
    $sql = "select s.*, p.display_name as parent_name, c.name as category_name 
                FROM Slot s 
                LEFT JOIN Slot p ON p.id = s.parent_id 
				LEFT JOIN Category c ON c.id = s.category_id
                WHERE s.is_active = true ";
    if ($parent_id)
      $sql .= " AND c.parent_id = ".$parent_id;
    $st = $con->execute($sql);
    $this->slots = $st->fetchAll();
    $this->slot_form = new SlotForm();
      
  	return sfView::SUCCESS;
  }
  
  public function executeEdit(sfWebRequest $request)
  {
    $data = $request->getParameter('slot', null);
    if ($data) {
        if (isset($data['id']) && $data['id']) {
          $slot = Doctrine_Core::getTable('slot')->find($data['id']);
          $notify = "update";
        } else {
          $slot = new slot();
          $notify = "create";
        }
        $slot->display_name = $data['display_name'];
		$slot->display_name_vi = $data['display_name_vi'];
		if ($slot->display_name == "")
			$slot->display_name = $slot->display_name_vi;
		else if ($slot->display_name_vi == "")
			$slot->display_name_vi = $slot->display_name;
        $slot->parent_id = $data['parent_id'];
        $slot->display_position = $data['display_position'];
        $slot->display_order = $data['display_order'];
        $slot->category_id = $data['category_id'];
        $slot->is_active = isset($data['is_active']) ? true : false;
		if ($slot->parent_id != 0)
		{
			$slot_parent = Doctrine_Core::getTable("Slot")->find($slot->parent_id);
			$slot->lshift = $slot_parent->lshift + 1;
		}
        $slot->save();
        $this->redirect('@slot_list?notify='.$notify.'_slot_success');
      } else {
        $this->slot_id = $request->getParameter('slot_id', null);
        if ($this->slot_id) { // it's an edit action
          $slot = Doctrine_Core::getTable('Slot')->find($this->slot_id);
          $this->slot_form = new SlotForm($slot);
        }
      }
    return sfView::SUCCESS;
  }
  public function executeDelete(sfWebRequest $request)
  {
    $slot_id = $request->getParameter('slot_id', null);
    $slot = Doctrine_Core::getTable('Slot')->find($slot_id);
    if ($slot) {
      $slot->deactivate();  
      $this->getRequest()->setParameter('notify', "slot_deactive_success");
    } else {
        $this->getRequest()->setParameter('notify', "slot_deactive_failed");
    }
    
    $this->forward('slot', 'list');
  }
}
